package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {
    /**
     * 登录：
     * 1.首先查看验证码是否正确
     * 2.如果正确，根据用户名和密码查询是否数据库有数据
     * 3.如果有，则提示登录成功，跳转到管理页面
     * 4.如果没有，则提示用户名或者账号不存在
     *
     * @param userModel
     * @return
     */
    @Override
    public String login(UserModel userModel) {
        if (userModel.getCode().equals(userModel.getSessionCode())) {
            UserDao userDao = new UserImpl();
            UserModel model = userDao.findByUsernameAndPassword(userModel);
            if (model == null) {
                return "error";
            } else {
                return "success";
            }
        } else {
            return "error-code";
        }

    }

    @Override
    public String add(UserModel userModel) {
        UserDao userDao = new UserImpl();
        return userDao.add(userModel) == 1 ? "success" : "error";
    }

    @Override
    public String update(UserModel userModel) {
        UserDao userDao = new UserImpl();
        return userDao.update(userModel) == 1 ? "success" : "error";
    }

    @Override
    public String delete(UserModel userModel) {
        UserDao userDao = new UserImpl();
        return userDao.delete(userModel) == 1 ? "success" : "error";
    }

    @Override
    public UserModel findById(int id) {
        UserDao userDao = new UserImpl();

        return userDao.findById(id);
    }

    @Override
    public List<UserModel> findAll(UserModel userModel) {
        UserDao userDao = new UserImpl();

        return userDao.findAll(userModel);
    }

    @Override
    public Integer getCount(UserModel userModel) {
        UserDao userDao = new UserImpl();
        return userDao.getCount(userModel);
    }
}
